import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
	state: {
		shoecabinet_id: '',
		tabbar: [{
				iconPath: "/static/tabBar/home.png",
				selectedIconPath: "/static/tabBar/home-a.png",
				text: '首页',
				pagePath: "/pages/index/index"
			},
			{
				iconPath: "/static/tabBar/order.png",
				selectedIconPath: "/static/tabBar/order-a.png",
				text: '商品',
				// midButton: true,
				pagePath: "/pages/product/product"
			},
			{
				iconPath: "/static/tabBar/sys.png",
				selectedIconPath: "/static/tabBar/sys.png",
				// text: '订单',
				midButton: true,
				// pagePath: "/pages/order/order"
			},
			{
				iconPath: "/static/tabBar/car.png",
				selectedIconPath: "/static/tabBar/car-a.png",
				text: '购物车',
				count: 0,
				pagePath: "/pages/car/car"
			},
			{
				iconPath: "/static/tabBar/my.png",
				selectedIconPath: "/static/tabBar/my-a.png",
				text: '我的',
				pagePath: "/pages/my/my"
			},
		],
		cartList: [], //购物车列表
		checkCart: {}, //检测是否是购物车数据的对象
		allSelected: false, // 全选检测
		cartNum: 0, //购物车,涉及到刷新数据丢失，所以存了本地,
		isScanCode: false
	},
	mutations: {
		// cart数据获取变动。
		CART_LIST(state, payload) {
			state.cartList = payload;
			let cartListNumber = payload.length;
			// console.log(cartListNumber)
			if (Number(cartListNumber)) {
				// uni.setTabBarBadge({
				// 	index: 2,
				// 	text: String(cartListNumber)
				// })
				state.tabbar[3].count = cartListNumber
			} else {
				// uni.removeTabBarBadge({
				// 	index: 2
				// })
				state.tabbar[3].count = 0
			}
			state.cartNum = cartListNumber;
		},

		// 切换全选。
		changeAllSellect(state) {
			state.allSelected = !state.allSelected;
		},
		// 全选设置
		getAllSellectCartList(state, flag) {
			state.cartList.map(item => {
				item.checked = flag
			})
		},
		// 全选检测
		checkCartList(state) {
			let all = true;
			state.cartList.map(item => {
				if (!item.checked) {
					all = false
				}
			})
			state.allSelected = all;
		},
		// 是否购物车数据检测
		getCheckCart(state) {
			let obj = {};
			state.cartList.map(item => {
				obj[item.goods_id] = {
					num: item.goods_num,
					cartOrderId: item.id
				};
			});
			state.checkCart = obj;
		},
	},
	actions: {
		
		// 购物车数据（查）
		getCartList({
			commit
		}) {
			return new Promise((resolve, reject) => {
				uni.$u.http.post('cart/cart_list').then(res => {
					let cartData = res.data.list || [];
					cartData.length && cartData.map(item => {
						item.checked = true;
					})
					commit('CART_LIST', cartData);
					commit('checkCartList');
					commit('getCheckCart')
				}).catch(e => {
					let cartData = []
					commit('CART_LIST', cartData);
					commit('checkCartList');
					commit('getCheckCart')
					reject(e)
				})
			})
		},
		// 添加到购物车（增）
		addCartGoods({
			dispatch
		}, data) {
			return new Promise((resolve, reject) => {
				uni.$u.http.post('cart/add_cart', {
					id: data.id,
					quantity: data.quantity
				}).then(res => {
					dispatch('getCartList');
					resolve(res)
				}).catch(e => {
					reject(e)
				})
			})
		},
		// 修改购物车商品数量（改）|| 删除购物车商品（删）
		changeCartList({
			commit,
			dispatch
		}, param) {
			return new Promise((resolve, reject) => {
				uni.$u.http.post('cart/cart_del', {
					id: param.ids
				}).then(res => {
					// if (res.code === 200) {
					dispatch('getCartList');
					// }
					commit('checkCartList');
					resolve(res)
				}).catch(e => {
					reject(e)
				})
			})
		},
	},
	getters: {
		cartList: state => state.cartList,
		checkCart: state => state.checkCart,
		allSelected: state => state.allSelected,
		cartNum: state => state.cartNum,
		// 购物车数量和总价
		totalCount: state => {
			let totalNum = 0;
			let totalPrice = 0;
			state.cartList.filter(item => {
				if (item.checked) {
					totalNum += 1;
					if (item.member_price) {
						totalPrice += item.quantity * parseFloat(item.member_price);
					} else {
						totalPrice += item.quantity * parseFloat(item.price);
					}

				}
			})
			return {
				totalNum,
				totalPrice
			}
		},

		// 是否选择了商品
		isSel: state => {
			let isSel = false;
			state.cartList.map(item => {
				if (item.checked) {
					isSel = true
				}
			})
			return isSel
		},

		// 活动商品唯一选中可以结算
		isActivityPay() {
			let num = 0
			let activityNum = 0
			state.cartList.map(item => {
				if (item.checked) {
					num += 1
					if (item.cart_type === 'activity') {
						activityNum += 1
					}
				}
			})
			if ((num === 1 && activityNum === 1) || activityNum === 0) {
				return true
			}
			return false
		}
	}
})
export default store
